package com.nic.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nic.view.R;
import com.nic.view.adapter.CommonCheckAdapter;
import com.nic.view.adapter.SingleChooseAdapter;
import com.nic.view.model.CommonChooseBean;
import com.nic.view.widget.MyGridView;

import java.io.Serializable;
import java.util.List;

public class SingleChooseActivity extends VBaseActivity implements View.OnClickListener, CommonCheckAdapter.OnClickItemSingleListener {
    private MyGridView gv;
    private Button bt_cancel;
    private Button bt_confirm;
    private Activity act;
    //    private View view;
    private TextView tvDes;
    private SingleChooseAdapter adapter;
    public static final String CHECKED_ID = "check_id";
    public static final String Tag = "tag";
    public static final String Title = "title";
    public static final String LIST = "list";
    public static final String ITEM = "item";
    private String tag;
    public static final String ACTION_SEND = "com.ajj.single.choose.send";

    private CommonChooseBean chooseBean;

//    private TagsLayout mTagsLayout;


    @Override
    public int getContentLayoutId() {
        return R.layout.activity_single_choose;
    }

    public void initView() {
        tvDes = (TextView) findViewById(R.id.tv_desc);
        gv = (MyGridView) findViewById(R.id.gv);
        bt_cancel = (Button) findViewById(R.id.cancel);
        bt_cancel.setOnClickListener(this);
        bt_confirm = (Button) findViewById(R.id.confirm);
        bt_confirm.setOnClickListener(this);
//        mTagsLayout = (TagsLayout) findViewById(R.id.tag_layout);


        adapter = new SingleChooseAdapter();
        adapter.setClickItemSingleListener(this);
        gv.setAdapter(adapter);

        initData();
    }

//    private void setTag(String text) {
//        CheckBox textView = new CheckBox(this);
//        textView.setText(text);
//        setButtonDrawableNull(textView);
//        textView.setPadding(5,5,5,5);
//        textView.setTextColor(getResources().getColor(R.color.tv_white_gray));
//        textView.setBackgroundResource(R.drawable.select_single_choose);
//        ViewGroup.MarginLayoutParams cparams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//
//        mTagsLayout.addView(textView, cparams);
//    }
//
//    private void setButtonDrawableNull(CheckBox checkBox) {
//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
//            try {
//                Field field = checkBox.getClass().getSuperclass().getDeclaredField("mButtonDrawable");
//                field.setAccessible(true);
//                field.set(checkBox, null);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

    private void initData() {
        if (getIntent().hasExtra(Tag)) {
            Intent intent = getIntent();
            try {
                List<CommonChooseBean> commonChooseBeans = (List<CommonChooseBean>) intent.getSerializableExtra(LIST);
                if (commonChooseBeans == null || commonChooseBeans.isEmpty()) {
                    finish();
                    return;
                }
                String title = intent.getStringExtra(Title);
                if (!TextUtils.isEmpty(title)) {
                    setToolbarTitle("" + title);
                }
                adapter.setList(commonChooseBeans);
                String checkId = intent.getStringExtra(CHECKED_ID);
                chooseBean = adapter.checked(checkId);
                tag = intent.getStringExtra(Tag);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cancel) {
            finish();
        } else if (i == R.id.confirm) {
            if (!TextUtils.isEmpty(tag) && chooseBean != null) {
                Intent intent1 = new Intent(SingleChooseActivity.ACTION_SEND);
                intent1.putExtra(ITEM, chooseBean);
                intent1.putExtra(Tag, tag);
                sendBroadcast(intent1);
            }
            finish();
        }
    }

    @Override
    public void onItemClick(int position) {
        chooseBean = adapter.getList().get(position);
    }

    /**
     * 单选页面
     *
     * @param tag               字典类型标签
     * @param title             字典类型名称
     * @param commonChooseBeans 字典数据
     */
    public static void jumpTo(Context context, String tag, String title, List<CommonChooseBean> commonChooseBeans) {
        jumpTo(context, tag, title, commonChooseBeans, null);
    }

    /**
     * 单选页面
     *
     * @param tag               字典类型标签
     * @param title             字典类型名称
     * @param commonChooseBeans 字典数据
     */
    public static void jumpTo(Context context, String tag, String title, List<CommonChooseBean> commonChooseBeans, String checkedId) {
        if (commonChooseBeans == null || commonChooseBeans.size() == 0) {
            Toast.makeText(context, "暂无数据内容", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent(context, SingleChooseActivity.class);
        intent.putExtra(LIST, (Serializable) commonChooseBeans);
        intent.putExtra(Title, title);
        intent.putExtra(CHECKED_ID, checkedId);
        intent.putExtra(Tag, tag);
        context.startActivity(intent);
    }

}
